package com.yhl.kafka;

import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: yanghl
 * @description: TODO
 * @date: 2024/3/26 16:31
 */
@RestController
@RequestMapping("/kafka/producer")
@Slf4j
public class ProducerController {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    private final String topicName = "hub-topic-city-01";
    @PostMapping("/test")
    public Object f01_1() {
        try {
            //1.获取业务数据
            // CityDTO cityDTO = CityDTO.buildDto(2023061501L, "杭州", "杭州是一个好城市");
            String cityStr = "杭州";
            log.info("向Kafka的Topic: {} ,写入数据:", topicName);
            log.info(cityStr);
            //2.使用KafkaTemplate向Kafka写入数据
            kafkaTemplate.send(topicName, cityStr);
        } catch (Exception e) {
            log.info("Producer写入Topic异常.");
            e.printStackTrace();
        }
        return "写入成功";
    }
}

